// import 'reflect-metadata'
import { Request, Response } from 'express'
import { controller, get, post } from '../decorator'
import { getResponseData } from '../utils/util'

interface BodyRequest extends Request {
  body: {
    [key: string]: string | undefined
  }
}

@controller('/api')
export class LoginController {
  @get('/isLogin')
  isLogin(req: BodyRequest, res: Response): void {
    const islogin = !!(req.session ? req.session.login : false)
    res.json(getResponseData<boolean>(islogin))
  }

  @post('/login')
  login(req: BodyRequest, res: Response): void {
    const islogin = !!(req.session ? req.session.login : false)
    if (islogin) {
      res.json(getResponseData<boolean>(true))
    } else {
      const { password } = req.body
      if (password === '123' && req.session) {
        req.session.login = true
        res.json(getResponseData<boolean>(false))
      } else {
        res.json(getResponseData<boolean>(false, '登录失败'))
      }
    }
  }
  @get('/logout')
  logout(req: BodyRequest, res: Response): void {
    if (req.session) {
      req.session.login = undefined
    }
    res.json(getResponseData<boolean>(true))
  }
}
